package com.hantang.web.json.handlers;

import org.glassfish.grizzly.http.server.Request;
import org.glassfish.grizzly.http.server.Response;

import com.google.gson.Gson;
import com.hantang.web.dao.ArticleDao;
import com.hantang.web.data.ArticleData;
import com.hantang.web.json.ds.Article;
import com.hantang.web.json.ds.MetaData;
import com.hantang.web.json.res.ArticleRes;

public class GetArticleHandler extends AbstractHandler {

	@Override
	public String doService(Request req, Response resp) throws Exception {
		String sid = req.getParameter("id");
		
		String sret = handleServlet(sid);
		
		return sret;
	}
	public static String handleServlet(String sid)
	{
		ArticleRes res = new ArticleRes();
		if(sid != null){
			long id = Long.valueOf(sid.trim());
			
			ArticleDao dao = new ArticleDao();
			ArticleData data = dao.loadData(id);
			Article article = Article.convertData2Json(data, Integer.MAX_VALUE);
			
			res.setData(article);
			res.setMeta(new MetaData(MetaData.META_DATA_SUCCESS_CODE,"Succ"));
			
		}else{
			res.setMeta(new MetaData(MetaData.META_DATA_ERROR_PARA, "Failed"));
		}
		return new Gson().toJson(res);
	}

	
}
